package com.zhengxing.jienci.user.controller.rbac;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zhengxing.jienci.user.domain.rbac.Role;
import com.zhengxing.jienci.user.domain.rbac.RoleResourceFunc;
import com.zhengxing.jienci.common.support.SimpleResponse;
import com.zhengxing.jienci.user.service.rbac.ResourceService;
import com.zhengxing.jienci.user.service.rbac.RoleResourceFuncService;
import com.zhengxing.jienci.user.service.rbac.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;

/**
 * RoleController
 *
 * @author rengh
 * @version 1.00
 */
@Slf4j
@Controller
@RequestMapping("/sys/roles")
@Lazy
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private RoleResourceFuncService roleResourceFuncService;

    @GetMapping("")
    public SimpleResponse pagination(@RequestParam(value = "name", defaultValue = "") String name,
                                     @RequestParam(value = "code", defaultValue = "") String code,
                                     @RequestParam(value = "orderBy", defaultValue = "createdAt asc") String orderBy,
                                     @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        HashMap<String, Object> paramMap = Maps.newHashMap();
        try {
            name = URLDecoder.decode(name, StandardCharsets.UTF_8.toString());
            paramMap.put("name", name);
            code = URLDecoder.decode(code, StandardCharsets.UTF_8.toString());
            paramMap.put("code", code);
        } catch (UnsupportedEncodingException e) {
            log.error(e.getLocalizedMessage());
        }

        SimpleResponse simpleResponse = new SimpleResponse();
        simpleResponse.put("data", roleService.getPagination(paramMap, orderBy, pageNo, pageSize));
        return simpleResponse;
    }

    @PostMapping("")
    public SimpleResponse save(@RequestBody Role role) {
        boolean bool = roleService.save(role);
        if (bool) return SimpleResponse.success();
        else return SimpleResponse.fail();
    }

    @PatchMapping("{roleId}")
    public SimpleResponse update(@PathVariable String roleId, @RequestBody Role role) {
        boolean bool = roleService.update(role);
        if (bool) return SimpleResponse.success();
        else return SimpleResponse.fail();
    }

    @DeleteMapping("{roleId}")
    public SimpleResponse delete(@PathVariable String roleId) {
        boolean bool = roleService.delete(roleId);
        if (bool) return SimpleResponse.success();
        else return SimpleResponse.fail();
    }

    @GetMapping("{resourceId}")
    public SimpleResponse view(@PathVariable String resourceId) {
        SimpleResponse simpleResponse = new SimpleResponse();
        simpleResponse.put("data", roleService.get(resourceId));
        return simpleResponse;
    }

    @GetMapping("nameExist")
    public boolean nameExist(String name, String id) {
        return !roleService.nameExist(name, id);
    }

    @GetMapping("codeExist")
    public boolean codeExist(String code, String id) {
        return !roleService.codeExist(code, id);
    }

    @GetMapping("findAllResourceTreeNodesWithFuncs")
    public SimpleResponse findAllResourceTreeNodesWithFuncs() {
        SimpleResponse simpleResponse = new SimpleResponse();
        simpleResponse.put("data", resourceService.findAllTreeNodesWithFuncs());
        return simpleResponse;
    }

    @GetMapping("findResourceFuncIds")
    public SimpleResponse findResourceFuncIds(String id) {
        List<String> resourceFuncIds = Lists.newArrayList();
        List<RoleResourceFunc> roleResourceFuncs = roleResourceFuncService.findByRoleId(id);
        roleResourceFuncs.forEach(roleResourceFunc -> resourceFuncIds.add(roleResourceFunc.getResourceFuncId()));

        SimpleResponse simpleResponse = new SimpleResponse();
        simpleResponse.put("data", resourceFuncIds);
        return simpleResponse;
    }

    @PostMapping("/perms")
    public SimpleResponse savePerms(String id, String[] perms) {
        roleResourceFuncService.savePerms(id, perms);
        return SimpleResponse.success();
    }

}
